package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.CheckItemService;

import java.util.List;

/**
 * ClassName: CheckItemServiceImpl
 * Description:
 * date: 2019/11/20 10:15
 * Author Airson
 * Since JDK 1.8
 */
@Service
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    CheckItemDao checkItemDao;

    @Override
    @Transactional
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        //条件查询
        Page<CheckItem> page = checkItemDao.findByCondition(queryPageBean.getQueryString());

        //返回结果
        return new PageResult(page.getTotal(),page);
    }

    @Override
    public void deleById(Integer id) {
        Long count = checkItemDao.findCountById(id);
        if (count >0){
            //说明有关联无法删除
            throw new RuntimeException("该检查项被检查组关联，不能删除！！");
        }else {
            //删除检查项
            checkItemDao.deleById(id);
        }
    }

    @Override
    public CheckItem findById(Integer id) {

        return checkItemDao.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
